//
//  QuotaAnalysis.m
//  cocoapodText
//
//  Created by apple on 16/9/10.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ShiPei.h"
#import "QuotaAnalysis.h"
#import <Foundation/Foundation.h>
@interface QuotaAnalysis ()

@end

@implementation QuotaAnalysis

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:FUIColorFromRGB(0xf4f4f4)];
    [self GetMainContentView];
}

/**
 * 边界
 * top:(0, 0, kScreenW, 10)
 * bottom:(0, kScreenH-10, kScreenW,10)
 * left:(0, 0, 10, kScreenH)
 * right:(kScreenW-170, 0, 10, kScreenH)
 */
-(void)GetMainContentView{
    //内容栏
    _quotaAnalysisView=[[QuotaAnalysisView alloc]initWithFrame:CGRectMake(0, 0, [ShiPei CGFloatMakeScale:TabChildContentW+10], kScreenH-CommonBarH-kStatusH-[ShiPei CGFloatMakeScale:FSCommonBottomH])];
    [_quotaAnalysisView setDelegate:self];
    [self.view addSubview:_quotaAnalysisView];
}

-(void)fristInitialLoad{
    APIQuotaAnalysisManager *manager=[APIQuotaAnalysisManager alloc];
    manager.delegate=self;
    [manager fsgy_api_quota_analysis_withFalg:QAInitTree];
    [_quotaAnalysisView setInitCondition];
}

-(void)onInitFristChartTableData:(QuotaAnalysisView *)quotaAnalysisView esCode:(NSString *)esCode{
    
}

-(void)onQuotaAnalysisManager:(APIQuotaAnalysisManager *)manager result:(NSDictionary *)result Flag:(NSString *)flag{
    if ([flag isEqualToString:QAInitTree]) {
        //地区目录
        NSArray *lstTree=[result objectForKey:@"lstTree"];
        [_quotaAnalysisView setGYMenuTableData:lstTree];
        //能源类型
        NSArray *typeMapList=[result objectForKey:@"typeMapList"];
        [_quotaAnalysisView setGYMenuEnergyTypeData:typeMapList];
    }else if([flag isEqualToString:QAInitChart]){
        [self onSetQuotaAnalysisChartData:_quotaAnalysisView BaseInfo:result];
    }
    
}

-(void)onQueryChartData:(QuotaAnalysisView*)quotaAnalysisView esCode:(NSString *)esCode xslx:(NSString *)xslx startTime:(NSString *)startTime endTine:(NSString *)endTime orgid:(NSString *)orgid structType:(NSString *)structType{
    APIQuotaAnalysisManager *manager=[APIQuotaAnalysisManager alloc];
    manager.delegate=self;
    [manager fsgy_api_quota_analysis_withFalg:QAInitChart esCode:esCode structType:structType xslx:xslx startTime:startTime endTime:endTime orgid:orgid];
}

-(void)onSetQuotaAnalysisChartData:(QuotaAnalysisView *)quotaAnalysisView BaseInfo:(NSDictionary *)data{
    NSArray *legend=[data objectForKey:@"legend"];
    NSArray *xAxis=[data objectForKey:@"xAxis"];
    NSArray *series=[data objectForKey:@"series"];
    //快速枚举遍历所有KEY的值
    //for (NSObject *object in data) {
    //NSLog(@"%@=%@",object,[data objectForKey:object]);
    //}
    
    //x轴文字
    NSMutableArray *xVals = [[NSMutableArray alloc] init];
    for (int i = 0; i < [xAxis count]; i++){
        [xVals addObject:[CommonTool getStringFromNSArray:xAxis key:i]];
    }
    //y轴多折线
    NSMutableArray *dataYVals = [[NSMutableArray alloc] init];
    
    for (int z = 0; z < [legend count]; z++){
        NSMutableArray *values = [[NSMutableArray alloc] init];
        NSMutableArray *yAxis=[[series objectAtIndex:z] objectForKey:@"data"];
        for (int i = 0; i < [xAxis count]; i++) {
            NSString *valStr=[CommonTool getStringFromNSArray:yAxis key:i];
            double val=[valStr doubleValue];
            [values addObject:[[ChartDataEntry alloc] initWithValue:val xIndex:i]];
        }
        [dataYVals addObject:values];
    }
    NSMutableArray *lineColors = [[NSMutableArray alloc] init];
    [lineColors addObject:FUIColorFromRGB(0x1f73af)];
    [lineColors addObject:FUIColorFromRGB(0x59cc9a)];
    
    quotaAnalysisView.xValsEnergyLine = xVals;
    quotaAnalysisView.yValsEnergyLine = dataYVals;
    quotaAnalysisView.lineColors=lineColors;
    quotaAnalysisView.title=[CommonTool getStringFromNSDictionary:data key:@"title" defaultValue:@""];
    //设置变化趋势曲线图数据
    [quotaAnalysisView multipleLineChartDataWithLegend:legend];
    [quotaAnalysisView setEnergyTableWithxAxis:xAxis yAxiss:series legend:legend];
}

-(void)onSetData:(QuotaAnalysisView *)quotaAnalysisView{
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self fristInitialLoad];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [_quotaAnalysisView dismissMenu];
}
@end
